<?php
/**
 * TomatoCMS
 *
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 *
 * @copyright    Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license        http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version     $Id: add.phtml 3032 2010-05-25 08:00:42Z huuphuoc $
 */
?>
<?php
$this->headTitle($this->translator('category_edit_page_title'));
$this->headScript()
    ->appendFile($this->APP_STATIC_SERVER . '/js/jquery.validate/jquery.validate.min.js')
    ->appendFile($this->APP_STATIC_SERVER . '/js/tomato/util.js');
?>

<div class="grid_12 t_a_ui_breadcrumb">

    <span><a href="<?php echo $this->url(array(), 'shop_category_list'); ?>"><?php echo $this->translator('category_list_breadcump'); ?></a></span>
    <span><?php echo $this->translator('category_edit_breadcump'); ?></span>
</div>

<div class="grid_12"><?php echo $this->flashMessenger(); ?></div>

<form method="post" action="<?php echo $this->url(array('category_id' => $this->id), 'shop_category_edit'); ?>" id="addCategoryForm">
<div class="grid_12 t_a_bottom"><?php echo $this->translator('category_add_guide'); ?></div>
<?php $category = $this->category;?>

<div class="grid_12 t_a_bottom">
    <div class="prefix_1 suffix_1">
        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('category_admin_name'); ?>:</label>
            <input maxlength="255" type="text" id="category_admin_name" name="category_admin_name" value="<?php echo $category->category_admin_name;?>" style="width: 300px" />
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('category_status'); ?>:</label>
            <select id="category_status" name="category_status" style="width:170px">
                <option value="">---</option>
                <option value="1" <?php if($category->category_status == 1){ echo "selected"; }?>><?php echo $this->translator('category_status_active'); ?></option>
                <option value="0" <?php if($category->category_status == 0){ echo "selected"; }?>><?php echo $this->translator('category_status_inactive'); ?></option>
            </select>
        </div>

        <hr/>

        <!--<div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('category_image'); ?>:</label></div>
            <?php
                /**
                * Allows user to browse uploaded file
                * @since 2.0.4
                */
                /*echo $this->browser('/upload/news/'.Zend_Auth::getInstance()->getIdentity()->user_name.'/'.date('Y').'/'.date('m'), 'jpg,gif,png', 'insertImage(%s)');*/
            ?>
        </div>-->

        <hr/>

        <div class="clearfix t_a_bottom">
            <label>&nbsp;</label>
            <input type="checkbox"  <?php if($category->category_enable_product == 1){ echo "checked"; }?> id="category_enable_product" name="category_enable_product" value="1" />&nbsp;
            <?php echo $this->translator('category_enable_product'); ?>
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('category_create_date'); ?>:</label>
            <?php $date  = strtotime($category->category_created_date); echo date('d/m/Y', $date)?>
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('category_modify_date'); ?>:</label>
            <?php echo $this->date;?>
        </div>

        <hr/>

        <div class="clearfix t_a_bottom">
            <label><?php echo $this->translator('category_parent'); ?>:</label>
            <select name="parentId" id="parentId" style="width:170px">
                <option value="">---</option>
                <?php for ($i = 0; $i < count($this->categories); $i++) : ?>
                <option<?php if ($this->category != null && $this->id == $this->categories[$i]->category_id) : ?> disabled<?php endif; ?> value="<?php echo $this->categories[$i]->category_id; ?>"<?php if ($this->category != null) : ?><?php if ($this->category->category_left > $this->categories[$i]->category_left && $this->category->category_right < $this->categories[$i]->category_right) : ?> selected<?php endif; ?><?php endif; ?>><?php echo str_repeat('---', $this->categories[$i]->depth).$this->categories[$i]->category_admin_name; ?></option>
                <?php endfor; ?>
            </select>
        </div>

        <hr/>

        <div class="grid_10 prefix_2 alpha">
            <button type="submit"><span><?php echo $this->translator('category_add_save_button'); ?></span></button>&nbsp;&nbsp;&nbsp;&nbsp;
            <button type="reset"><span><?php echo $this->translator('category_reset_button'); ?></span></button>
        </div>
    </div>
</div>
</form>
<?php $arrCategoryByLanguage = $this->arrCategoryByLanguage?>
<?php foreach ($this->languages as $row):?>
<hr/>
<!--language default content-->
<form method="post" action="<?php echo $this->url(array('category_id' => $this->id), 'shop_category_save_content'); ?>" id="addCategoryLanguageForm_<?php echo $row->language_code;?>">
<div class="grid_12 t_a_bottom">
    <input type="hidden" id="category_content_id" name="category_content_id" value="<?php echo @$arrCategoryByLanguage[$row->language_code]->category_content_id?>" />
    <div class="clearfix t_a_bottom">
        <?php echo $this->translator('category_language_guide'); ?> <strong><?php echo $row->language_name;?></strong>
        <input type="hidden" id="language_code" name="language_code" value="<?php echo $row->language_code;?>" />
    </div>

    <div class="prefix_1 suffix_1">

    <div class="clearfix t_a_bottom">
        <label><?php echo $this->translator('category_name'); ?>:</label>
        <input maxlength="255" type="text" id="category_content_name" name="category_content_name" value="<?php echo @$arrCategoryByLanguage[$row->language_code]->category_content_name?>"  style="width: 300px" />
    </div>

    <hr/>

    <div class="clearfix t_a_bottom">
        <label><?php echo $this->translator('category_friendly_url'); ?>:</label>
        <input type="text" id="category_content_friendly_url" name="category_content_friendly_url" value="<?php echo @$arrCategoryByLanguage[$row->language_code]->category_content_friendly_url?>"  style="width: 300px" />
    </div>

    <hr/>

    <div class="clearfix t_a_bottom">
        <label><?php echo $this->translator('category_description'); ?>:</label>
        <textarea id="category_content_description" name="category_content_description" style="width: 300px; height: 100px;">
            <?php echo @$arrCategoryByLanguage[$row->language_code]->category_content_description?>
        </textarea>
    </div>

    <hr/>

    <div class="grid_10 prefix_2 alpha">
        <button type="submit"><span><?php echo $this->translator('category_add_save_button'); ?></span></button>&nbsp;&nbsp;&nbsp;&nbsp;
        <button type="reset"><span><?php echo $this->translator('category_reset_button'); ?></span></button>
    </div>

    </div>
</div>
</form>
<?php endforeach;?>

<script type="text/javascript">

function insertImage(url) {
    tinyMCE.getInstanceById('content').execCommand('mceInsertContent', false, '<img src="' + url + '" />');
};

function normalizeSlug(str) {
    str = str.replace(/^\s+|\s+$/g, '');
    var from = "ÁÀẠẢÃĂẮẰẶẲẴÂẤẦẬẨẪáàạảãăắằặẳẵâấầậẩẫóòọỏõÓÒỌỎÕôốồộổỗÔỐỒỘỔỖơớờợởỡƠỚỜỢỞỠéèẹẻẽÉÈẸẺẼêếềệểễÊẾỀỆỂỄúùụủũÚÙỤỦŨưứừựửữƯỨỪỰỬỮíìịỉĩÍÌỊỈĨýỳỵỷỹÝỲỴỶỸĐđÑñÇç·/_,:;";
    var to   = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaooooooooooooooooooooooooooooooooooeeeeeeeeeeeeeeeeeeeeeeuuuuuuuuuuuuuuuuuuuuuuiiiiiiiiiiyyyyyyyyyyddnncc------";

    for (var i = 0, l = from.length ; i < l; i++) {
        str = str.replace(new RegExp(from[i], "g"), to[i]);
    }
    str = str.replace(/[^a-zA-Z0-9 -]/g, '').replace(/\s+/g, '-').toLowerCase();
    return str;
};

$(document).ready(function() {

    $('#addCategoryForm').validate({
        rules: {
            category_admin_name: {
                required: true,
                maxlength: 255
            },
            category_status:{
                required: true,
            }
        },
        messages: {
            category_admin_name: {
                required: "<?php echo $this->translator('category_name_required'); ?>",
                maxlength: "<?php echo $this->translator('category_name_maxlength'); ?>"
            },
            category_status: "<?php echo $this->translator('category_status_required'); ?>"
        }
    });

    $.validator.addMethod('slug', function(value) {
        value = value.replace(/^\s+|\s+$/g, '');
        var regex = new RegExp(/^[a-z0-9\-]+$/);
        if (value == '' || value.match(regex)) {
            return true;
        } else {
            return false;
        }
    });

    <?php foreach ($this->languages as $row):?>
        $('#addCategoryLanguageForm_<?php echo $row->language_code;?>').validate({
        rules: {
            category_content_name: {
                required: true,
                maxlength: 255
            },
            category_content_friendly_url: {
                slug: true,
                maxlength: 255
            }
        },
        messages: {
            category_content_name: {
                required: "<?php echo $this->translator('category_content_name_required'); ?>",
                maxlength: "<?php echo $this->translator('category_content_name_maxlength'); ?>"
            },
            category_content_friendly_url: {
                slug: "<?php echo $this->translator('catgory_content_friendly_url_validate'); ?>",
                maxlength: "<?php echo $this->translator('catgory_content_friendly_url_maxlength'); ?>"
            }
        }
    });
    <?php endforeach;?>

    <?php foreach ($this->languages as $row):?>
        $('#addCategoryLanguageForm_<?php echo $row->language_code;?> #category_content_name').bind('change', function() {
            $('#addCategoryLanguageForm_<?php echo $row->language_code;?> #category_content_friendly_url').val(normalizeSlug($(this).val()));
        });
    <?php endforeach;?>
});
</script>